package java.Bad.Test_Cast_ID_2168;

/*
 * This software was developed at the National Institute of Standards and
 * Technology by employees of the Federal Government in the course of their
 * official duties. Pursuant to title 17 Section 105 of the United States
 * Code this software is not subject to copyright protection and is in the
 * public domain. NIST assumes no responsibility whatsoever for its use by
 * other parties, and makes no guarantees, expressed or implied, about its
 * quality, reliability, or any other characteristic.
 *
 * This reference program was developed in Jan 2010 as part of the Software
 * Assurance Metrics And Tool Evaluation (SAMATE) project.
 * We would appreciate acknowledgment if the software is used.
 * The SAMATE project website is: http://samate.nist.gov
 */

/* This code (java servlet) has a Failure to Preserve Web Page Structure
 * 'Cross-site Scripting (XSS)' CWE-79 vulnerability ( http://cwe.mitre.org )
 *
 * This code demos the Reflected XSS (or Non-Persistent) ==> "Container Bad Case".
 * The servlet reads the user input from the HTTP request and directly reflects
 * the user input back into the HTTP response.
 *
 */

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class cwe79_reflectedXSS_bad_container extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");

		PrintWriter out = response.getWriter();

		out
				.println("<html>"
						+ "<head><title>CWE79 Reflected (Container) XSS Bad</title></head>");
		out.println("<body  bgcolor=\"#ffffff\">"
				+ "<h4>Hello, my name is Test4XSS. What's yours?</h4>"
				+ "<form method=\"get\">"
				+ "<input type=\"text\" name=\"username\" size=\"50\">"
				+ "<p></p>" + "<input type=\"submit\" value=\"Submit\">"
				+ "<input type=\"reset\" value=\"Reset\">" + "</form>");

		String container_username[] = new String[1];
		container_username[0] = request.getParameter("username"); /* Container */

		if ((container_username[0] != null)
				&& (container_username[0].length() > 0)) {
			out.println("<h4>Hello, ");
			out.println(container_username[0]); /*
												 * BAD - output user's input
												 * without filtering
												 */
			out.println("!</h4>");
		}
		out.println("</body></html>");
		out.close();
	}

	public String getServletInfo() {
		return "The Hello servlet says hello (with CWE79 Reflected XSS (Container) bad sample code).";
	}

}
